package com.ai.smlcore.util;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Map;

import com.ai.frame.logger.Logger;
import com.ai.frame.logger.LoggerFactory;

/**
 * 写文件工具类
 *
 */
public class WriteFile {
	private static final Logger logger = LoggerFactory.getUtilLog(WriteFile.class);

	/**
	 * 将数据结果写入文件
	 *
	 */
	public static void writeResult(Map<File, String> results) {
		for (Map.Entry<File, String> entry : results.entrySet()) {
			FileOutputStream fileOutputStream = null;
			try {
				File file = entry.getKey();
				logger.info("path:", file.getAbsolutePath() + "==========fileName:" + file.getName());
				if (file.exists()) {
					logger.info("statu", "File is exist.....");
					file.createNewFile();
				} else {
					logger.info("statu", "File is is not exist....");
					if (file.createNewFile()) {
						logger.info("statu", "File is create success!!!");
					} else {
						logger.info("statu", "File is create fail!!!");
						return;
					}
				}
				fileOutputStream = new FileOutputStream(file, true);
				OutputStreamWriter write = new OutputStreamWriter(fileOutputStream, "UTF-8");
				BufferedWriter output = new BufferedWriter(write);
				output.write(entry.getValue());
				output.newLine();
				output.close();
			} catch (Exception e) {
				logger.error("写文件异常信息", e.getMessage(), e);
			} finally {
				if (fileOutputStream != null) {
					try {
						fileOutputStream.close();
					} catch (IOException e) {
						logger.error("", "", e);
					}
				}
			}
		}
	}
}
